package com.ozy.kxreader.base

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import com.ozy.kxreader.R
import com.ozy.kxreader.app.ActivityLifecycleable
import com.ozy.kxreader.di.component.AppComponent
import com.ozy.kxreader.getAppComponent
import com.trello.rxlifecycle2.android.ActivityEvent
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import javax.inject.Inject

/**
 * Created by 欧源 on 2018/1/30.
 */
abstract class BaseActivity<P : IPresenter> : AppCompatActivity(), ActivityLifecycleable {


    private val mLifecycleSubject: BehaviorSubject<ActivityEvent> = BehaviorSubject.create()


    fun getLifecycleSubject(): Subject<ActivityEvent> = mLifecycleSubject


    @Inject
    lateinit var mPresenter: P

    var toolbar: Toolbar? = null
        private set

    private var mToolbarTitle: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId())
        setupActivityComponent(getAppComponent())
        initData(savedInstanceState)
    }


    override fun setContentView(layoutResID: Int) {
        super.setContentView(layoutResID)
        setupToolbar();
    }

    private fun setupToolbar() {
        toolbar = findViewById(R.id.toolbar)
        if (toolbar == null) return
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(false)
        supportActionBar?.setHomeButtonEnabled(false)
        toolbar?.setContentInsetsAbsolute(0, 0)
        val btnBack = toolbar!!.findViewById<View>(R.id.toolbar_back)
        btnBack?.setOnClickListener { v -> finish() }
    }

    override fun setSupportActionBar(toolbar: Toolbar?) {
        super.setSupportActionBar(toolbar)
        if (toolbar != null) {
            if (toolbar != null) {
                mToolbarTitle = toolbar.findViewById(R.id.toolbar_title)
                if (mToolbarTitle != null) {
                    supportActionBar!!.setDisplayShowTitleEnabled(false)
                }
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if (id == android.R.id.home) {
            finish()
            return true
        }

        return super.onOptionsItemSelected(item)
    }

    override fun onTitleChanged(title: CharSequence, color: Int) {
        super.onTitleChanged(title, color)
        if (mToolbarTitle != null) {
            mToolbarTitle!!.text = title
        }
    }

    abstract fun layoutId(): Int

    abstract fun setupActivityComponent(appComponent: AppComponent?)

    abstract fun initData(savedInstanceState: Bundle?)

    override fun provideLifecycleSubject(): Subject<ActivityEvent> = mLifecycleSubject
}